<template>
  <div id="login3">
    <div class="container3">
      <div class="title3">用户注册</div>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="uname">
          <el-input placeholder="请输入账号" prefix-icon="el-icon-user" size="medium" v-model="form.uname"></el-input>
        </el-form-item>
        <el-form-item prop="upwd">
          <el-input placeholder="请输入密码" show-upwd prefix-icon="el-icon-lock" size="medium"
                    v-model="form.upwd"></el-input>
        </el-form-item>
<!--        <el-form-item prop="phone">-->
<!--          <el-input placeholder="请输入联系方式" prefix-icon="el-icon-phone" size="medium"-->
<!--                    v-model="form.phone"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item prop="email">
          <el-input placeholder="请输入QQ邮箱" prefix-icon="el-icon-message" size="medium"
                    v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item prop="verificationCode">
          <div class="verification-code-wrapper3">
            <el-input placeholder="请输入验证码" prefix-icon="el-icon-s-check" size="medium"
                      v-model="form.verificationCode"></el-input>
            <el-button size="medium" type="primary" :disabled="isSending" @click="sendVerificationCode"
                       :style="buttonStyle">{{ buttonLabel }}</el-button>
          </div>
        </el-form-item>
        <div class="button-row3">
          <el-button size="medium" type="primary" @click="register" :style="buttonStyle3">注册</el-button>
        </div>
        <div class="button-row3">
          <el-button size="medium" type="primary" @click="retur" :style="buttonStyle3">
            返回登录页面
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "LoginRegister",
  data() {
    const checkPhone = (rule, value, callback) => {
      if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(value)) {
        callback(new Error('请输入合法的手机号'));
      }
      callback()
    };
    return {
      form: {},
      rules: {
        uname: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          // { max: 6, message: '字符长度最大为6位', trigger: 'blur'}
        ],
        upwd: [
          {required: true, message: '请输入密码', trigger: 'blur'},

          {min: 6, message: '请输入长度6以上的字符', trigger: 'blur'}
        ],
        phone: [
          { required: true, message: '请输入联系方式', trigger: 'blur' },
          {validator: checkPhone, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          {
            pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
            message: '请输入有效的邮箱地址',
            trigger: ['blur', 'change']
          }],

      },
      isSending: false, // 是否正在发送验证码
      countDown: 60, // 倒计时时间（秒）
    };
  },
  computed: {
    buttonStyle3() {
      const inputWidth = 300; // 输入框的宽度
      const buttonPadding = 10; // 按钮的内边距
      const buttonIconWidth = 14; // 按钮图标的宽度
      return `width: calc(${inputWidth}px - ${buttonPadding * 2}px - ${buttonIconWidth}px)`;
    },
    buttonLabel() {
      if (this.isSending) {
        return `${this.countDown}秒后可重发`;
      } else {
        return '发送验证码';
      }
    },
  },
  methods: {
    sendVerificationCode() {
      // 检查表单是否有效
      this.$refs.form.validateField('email', (errorMsg) => {
        if (errorMsg) {
          return;
        }

        //发送验证码
        request.post('send/email', this.form).then((res) => {
          if (res.code === '200') {
            // 发送成功后启用倒计时
            this.startCountdown();
            this.$notify.success("验证码已发送，请注意查收！");
          } else {
            this.$notify.error(res.msg);
          }
        });
      });
    },
    retur(){
      this.$router.push('/login');
    },
    startCountdown() {
      this.isSending = true; // 启用倒计时按钮
      const timer = setInterval(() => {
        if (this.countDown > 0) {
          this.countDown--;
        } else {
          clearInterval(timer);
          this.isSending = false; // 结束倒计时，启用发送按钮
          this.countDown = 60; // 重设倒计时时间
        }
      }, 1000);
    },
    register() {
      // 提交表单逻辑
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 发起注册请求s
          request.post('user/register', this.form).then((res) => {
            console.log(res);
            if (res.code === '200') {
              this.$notify.success("注册成功！");
              this.form={}
            } else {
              this.$notify.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>

<style scoped>
#login3 {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.container3 {
  width: 400px;
  height: 400px;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
  background-color: #fff;
}

.title3 {
  text-align: center;
  font-size: 24px;
  color: dodgerblue;
  margin-bottom: 20px;
}

.verification-code-wrapper3 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  gap: 70px;
}

.button-row3 {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

</style>
